#pragma once
#pragma once
#include <sys/select.h>
#include <array>
/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/epoll.h>
#include "Socket.hpp"
const int sz = 1024;
class Epoller
{
public:
    Epoller()
    {
        _epollfd = epoll_create(sz); // 创建epoll模型 --- 红黑树 read_queue 回调机制
    }
    int Wait(struct epoll_event *events, int mx, int time)
    {
        int n = epoll_wait(_epollfd, events, mx, time);
        return n;
    }
    void Add(int fd, int events)
    {
        struct epoll_event event;
        event.data.fd = fd;
        event.events = events;
        int n = epoll_ctl(_epollfd, EPOLL_CTL_ADD, fd, &event);
        if (n < 0)
        {
            std::cout << "epoll_ctl_add error : " << strerror(errno) << "\n";
        }
    }
    void Delete(int fd)
    {
        int n = epoll_ctl(_epollfd, EPOLL_CTL_DEL, fd, nullptr);
        if(n < 0)
        {
            std::cout << "epoll_ctl_delete error : " << strerror(errno) << "\n";
        }
    }
    void Modify(int fd, int events)
    {
        struct epoll_event event;
        event.data.fd = fd;
        event.events = events;
        epoll_ctl(_epollfd, EPOLL_CTL_MOD, fd, &event);
    }
    ~Epoller()
    {
        if (_epollfd >= 0)
            close(_epollfd);
    }

private:
    int _epollfd; // 默认是水平触发
};